package com.briup.day41;

import java.util.List;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Scanner;


/**
 * 版本3：使用更高级的字符流进行数据传输
 * 
 * 使用了PrintWriter每次输出一行消息。
 * 使用了BufferedReader每次读一行消息。
 * 避免了需要关闭输出流才能返回-1的问题。
 * 
 * @author Briup
 *
 */
public class ChatServer3 {

	public static void main(String[] args) {
		// 创建并开启服务器
		int port = 8888;
		try {
			ServerSocket ss = new ServerSocket(port);
			System.out.println("服务器已启动，端口号：" + port);
			System.out.println("等待客户端的连接....");
			// 进入侦听状态等待客户端的连接
	
	/**		Socket socket1 = ss.accept();
			System.out.println("一台客户端已连接！");
			
			Socket socket2 = ss.accept();
			System.out.println("另一台客户端已连接！");
		*/
			List<Socket> clients = new ArrayList<>();
			while (true) {
				Socket socket = ss.accept();
				clients.add(socket);
				new ClientThread(socket, clients).start();
		
				/**		// 接收客户端发送的信息
				BufferedReader br = new BufferedReader(
						new InputStreamReader(socket.getInputStream()));
				
				// 向客户端回复一条信息
				PrintWriter pw = new PrintWriter(socket.getOutputStream(), true);
				Thread t1 = new Thread() {
					@Override
					public void run() {
						
							} catch (IOException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}	
					}
				};
			*/	
				
			}

			
		/**	// 接收客户端发送的信息
			BufferedReader br1 = new BufferedReader(
					new InputStreamReader(socket1.getInputStream()));
			
			// 向客户端回复一条信息
			PrintWriter pw1 = new PrintWriter(socket1.getOutputStream(), true);
			
			BufferedReader br2 = new BufferedReader(
					new InputStreamReader(socket2.getInputStream()));
			
			// 向客户端回复一条信息
			PrintWriter pw2 = new PrintWriter(socket2.getOutputStream(), true);
			
			
			Scanner sc = new Scanner(System.in);
			
			/*while(true) {
				System.out.println("【客户端说】" + br.readLine());
				pw.println(sc.nextLine());
			}
			
			
			
			
			Thread t2 = new Thread() {
				@Override
				public void run() {
					while (true) {
						try {
							String msg =  br2.readLine();
							pw1.println(msg);
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}	
				}
			};
			
			t1.start();
			t2.start();
			*/
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

class ClientThread extends Thread{
	Socket socket;
	List<Socket> clients;
	
	public ClientThread(Socket socket,List<Socket> clients) {
		this.socket = socket;
		this.clients = clients;
	}
	
	@Override
	public void run() {
	
		try {
			InputStream is = socket.getInputStream();
			BufferedReader br = new BufferedReader(
					new InputStreamReader(is));
			while(true) {
				String msg = br.readLine();
				// 转发给其他客户端
				for (Socket client : clients) {
					if (client == socket) {
						continue;
					}
					PrintWriter pw = new PrintWriter(client.getOutputStream(), true);
					pw.println(msg);
				}
			}
			
		} catch (IOException e) {
			
		}
	}
}

